package com.lcm.weam.controller.pro;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lcm.weam.entity.pro.Member;
import com.lcm.weam.entity.resp.PageResult;
import com.lcm.weam.entity.resp.Result;
import com.lcm.weam.entity.resp.ResultCode;
import com.lcm.weam.service.pro.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 项目成员接口
 *   1、添加新成员
 *   2、修改成员信息
 *   3、删除成员
 *   4、查询改项目下的所有成员
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/pro")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 1、添加新成员
     */
    @RequestMapping(value = "{proid}/addMember", method = RequestMethod.POST)
    public Result addMember(@RequestBody Member member, @PathVariable String proid){
        member.setProid(proid);
        if ("".equals(member.getTime()) || member.getTime() == null){
            member.setTime(new Date());
        }
        memberService.createMember(member);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 2、修改成员信息
     */
    @RequestMapping(value = "/editMember", method = RequestMethod.PUT)
    public Result editMember(@RequestBody Member member){
        memberService.editMember(member);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 3、删除成员
     */
    @RequestMapping(value = "/deleteMember", method = RequestMethod.DELETE)
    public Result deleteMember(@RequestParam(value = "mid",required = true) int mid){
        memberService.deleteMember(mid);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 4、查询改项目下的所有成员
     */
    @RequestMapping(value = "{proid}/selectMembers", method = RequestMethod.GET)
    public Result selectMembers(@PathVariable String proid, int pageIndex, int pageSize){
        IPage<Member> page = memberService.selectMembers(proid, pageIndex, pageSize);
        return new Result(ResultCode.SUCCESS, new PageResult<Member>((int)page.getTotal(),(int) page.getPages(),page.getRecords()));
    }

}
